//
//  ViewController.m
//  scroll嵌套Table+悬停效果
//
//  Created by chenleping on 2018/6/21.
//  Copyright © 2018年 IAPTest. All rights reserved.
//

#import "ViewController.h"

#import "ContentScrollView.h"
#import "BottomScrollView.h"
//#import "Masonry.h"

#define MAXOFFSETY 200  // 最大偏移量
#define StatusH [UIApplication sharedApplication].statusBarFrame.size.height
#define NavigationBarH self.navigationController.navigationBar.frame.size.height

@interface ViewController ()<UIScrollViewDelegate>
{
    BOOL canMove;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    ContentScrollView * scrollView = [[ContentScrollView alloc] initWithFrame:CGRectMake(0, StatusH + NavigationBarH, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height-StatusH-NavigationBarH)];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height + 200);
    scrollView.backgroundColor = [UIColor greenColor];
    canMove = YES;  // 最开始的时候是可以滑动的
    
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    [scrollView addSubview:topView];
    topView.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.4];
    
    BottomScrollView * bottomView = [[BottomScrollView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, scrollView.frame.size.height)];
    [scrollView addSubview:bottomView];
    bottomView.didSelectedBlock = ^{
        ViewController * vc = [ViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canMove) name:@"OtherCanMove" object:nil];
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}
- (void)canMove{
    canMove = YES;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat contentOffsetY = scrollView.contentOffset.y;
    CGFloat maxOffsetY = MAXOFFSETY;
    if (contentOffsetY >= maxOffsetY) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"contentCanMove" object:nil]; // 告诉底部内容视图能进行滑动了
        canMove = NO;   // 自己不能滑动了
    }
    if (canMove == NO) {
        [scrollView setContentOffset:CGPointMake(0, maxOffsetY)];
    }
}



@end
